package com.example.contadorcomida;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

public class PaginaInicialActivity extends ActivityBasic {
	private boolean habilitoConfig=false; 
	
	// LISTENER
	private OnClickListener listenerEmpezar = new OnClickListener() {
		@Override
		public void onClick(View v) {
			EditText editTextAprox = (EditText) findViewById(R.id.cantAprox);
			int value = Integer.valueOf(editTextAprox.getText().toString()); 
			Intent intent = new Intent(PaginaInicialActivity.this, MainActivity.class); 
			intent.putExtra("cantidad", value); 
			startActivity(intent); 
			guardarPreferences(value); 
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main); 
		findViewById(R.id.btnEmpezar).setOnClickListener(this.listenerEmpezar);
		
		int valorGuardado = PreferenceManager.getDefaultSharedPreferences(this).getInt("cantidad", 0); 
		
		EditText cantidad = (EditText) findViewById(R.id.cantAprox); 
		cantidad.setText(String.valueOf(valorGuardado)); 
	}
	
	protected void guardarPreferences (int cantidad){
		super.onStop(); 
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
		
		SharedPreferences.Editor editor = preferences.edit(); 
		
		editor.putInt("cantidad", cantidad); 
		editor.commit(); 
	}

	public boolean getHabilitoConfig() {
		return habilitoConfig;
	}

	public void setHabilitoConfig(boolean habilitoConfig) {
		this.habilitoConfig = habilitoConfig;
	}
}
